﻿using System;
using System.Net;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkContract;
using System.Web.Helpers;
using SilenthinkContract.Domain;
using SilenthinkCommon.Extension;
using SilenthinkCommon.Components;
using SilenthinkCommon.ActionResults;

namespace Web.Controllers
{
    public abstract class BaseController : Controller
    {
        protected ILogService LogService { get; private set; }
        protected IBaseService BaseService { get; private set; }
        protected IExtraService ExtraService { get; private set; }
        protected IStoreService StoreService { get; private set; }
        protected IOrderService OrderService { get; private set; }
        protected IGameService GameService { get; private set; }
        protected IAuctionService AuctionService { get; private set; }
        protected IPassportService PassportService { get; private set; }
        protected IPaymentService PaymentService { get; private set; }
        protected static IMemcachedClient MemcachedClient { get; private set; }

        protected static string DefaultUrl = WebSite.Main;
        protected BaseController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
        {
            LogService = logService;
            BaseService = baseService;
            ExtraService = extraService;
            StoreService = storeService;
            OrderService = orderService;
            GameService = gameService;
            AuctionService = auctionService;
            PassportService = passportService;
            PaymentService = paymentService;
            MemcachedClient = memcachedClient;
        }

        protected ActionResult Captcha(string captchaKey)
        {
            return new CaptchaImageResult(captchaKey);
        }

        protected bool ValidateCaptcha(string captchaKey, string captchaTxt)
        {
            if (Session != null)
            {
                return Session[captchaKey].ToString() == captchaTxt.ToLower();
            }
            return false;
        }

        protected bool SendEmail(string address, EmailAccount mail, EmailTemplate template)
        {
            bool result;
            WebMail.EnableSsl = mail.EnableSsl;
            WebMail.SmtpServer = mail.Host;
            WebMail.SmtpPort = mail.Port;
            WebMail.From = mail.Email;
            WebMail.UserName = mail.Username;
            WebMail.Password = mail.Password;
            WebMail.SmtpUseDefaultCredentials = mail.UseDefaultCredentials;

            try
            {
                WebMail.Send(address, template.Subject, template.Body);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        protected void SendMobileMessage(string message, params string[] mobile)
        {
            using (var client = new WebClient())
            {
                client.BaseAddress = "";
                client.QueryString.Add("username", "");
                client.DownloadString("");
            }
        }

        protected string GetUserAgent()
        {
            var osVersion = "未知";

            var userAgent = Request.UserAgent ?? string.Empty;

            if (!userAgent.IsNullOrEmpty())
            {
                if (userAgent.Contains("NT 6.1"))
                {
                    osVersion = "Windows 7";
                }
                else if (userAgent.Contains("NT 6.0"))
                {
                    osVersion = "Windows Vista/Server 2008";
                }
                else if (userAgent.Contains("NT 5.2"))
                {
                    osVersion = "Windows Server 2003";
                }
                else if (userAgent.Contains("NT 5.1"))
                {
                    osVersion = "Windows XP";
                }
                else if (userAgent.Contains("NT 5"))
                {
                    osVersion = "Windows 2000";
                }
                else if (userAgent.Contains("NT 4"))
                {
                    osVersion = "Windows NT4";
                }
                else if (userAgent.Contains("Me"))
                {
                    osVersion = "Windows Me";
                }
                else if (userAgent.Contains("98"))
                {
                    osVersion = "Windows 98";
                }
                else if (userAgent.Contains("95"))
                {
                    osVersion = "Windows 95";
                }
                else if (userAgent.Contains("Mac"))
                {
                    osVersion = "Mac";
                }
                else if (userAgent.Contains("Unix"))
                {
                    osVersion = "UNIX";
                }
                else if (userAgent.Contains("Linux"))
                {
                    osVersion = "Linux";
                }
                else if (userAgent.Contains("SunOS"))
                {
                    osVersion = "SunOS";
                }
            }
            return osVersion;
        }

        /// <summary>
        ///  获取浏览器名称 版本号
        /// </summary>
        /// <returns></returns>
        protected string GetBrowser()
        {
            var browser = Request.Browser;
            return browser.Browser + browser.Version;
        }

        protected string GetIpAddress()
        {
            var result = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                result = Request.UserHostAddress;
            }
            return result;
        }

        protected string GetUrlReferrer()
        {
            var referrer = Request.UrlReferrer;
            if (referrer != null)
            {
                return referrer.ToString();
            }
            return string.Empty;
        }
    }
}
